﻿using SharpDepend;
using System;
using System.Windows.Threading;

namespace SharpDepend.WP8
{
    internal class GUITimerManager : IGUITimerManager
    {
        class GuiTimer : IGuiTimer
        {
            private DispatcherTimer mNativeTimer;
            private TimeSpan mInterval;

            public GuiTimer()
            {
                mNativeTimer = new DispatcherTimer();
            }

            public TimeSpan Interval
            {
                get
                {
                    return mInterval;
                }
                set
                {
                    mInterval = value;
                    mNativeTimer.Interval = value;
                }
            }

            public event Action Tick;

            public void Start()
            {
                mNativeTimer.Interval = mInterval;
                mNativeTimer.Tick += Loop;
                mNativeTimer.Start();
            }

            public void Stop()
            {
                mNativeTimer.Stop();
            }

            private void Loop(object sender, EventArgs e)
            {
                if (Tick != null)
                {
                    Tick();
                }
            }
        }

        public IGuiTimer CreateTimer()
        {
            return new GuiTimer();
        }
    }
}
